/**
 * User: antosha
 * Date: 05.08.12
 */
(function($) {
    $.fn.scrollBar = function() {
        var o = arguments[0] || {};

        this.each(function () {
            bindScrolling($(this));
        });
    };

    function bindScrolling(obj) {
        obj.css({'overflow': 'auto'});

        var wrapper = $('<div class="wrapper"></div>');
        wrapper.css({'overflow': 'hidden'});
        obj.wrap(wrapper);

        var scrollBar = $('<div class="scrollBarMain" style="position: absolute; top: 0px; display: inline-block; background-color: #F0F0F0">' +
                          '<div class="scrollBar" style="position: relative; background-color: gray; top: 0px; border-radius: 4px"></div></div>');
        $('.wrapper').append(scrollBar);

        var width = $('.wrapper').width(), contentWidth = $(".content").width(), scrlWdth = 16, offWidth = 5, oldS = -1;

        $(".content").width(contentWidth + offWidth);//16
        $(".scrollBarMain").width(scrlWdth - offWidth);
        $(".scrollBarMain").css('left', width - (scrlWdth - offWidth) + 0);

        // prevent highlight dragging from scrolling the wrapper div (thereby displaying the bars)
        $(".wrapper").scroll(function () {
            this.scrollLeft = 0;
            this.scrollTop = 0;
        });

        $(".content").scroll(function () {
            var s = this.scrollTop > oldS ? 1 : -1;
            var h = $('.scrollBar').height(), ch = $('.content').height();
            var t = $('.scrollBar').position().top;
            var top = t + s * h / 2, zz = ch - top;
            top = zz < h ? ch - h : top;
            top = top < 0 ? 0 : top;

            $('.scrollBar').css('top', top);
            oldS = this.scrollTop;
        });

        $(".scrollBar").click(function (e) {
            return false;
        });

        $(".scrollBarMain").click(function (e) {
            ;//$('.content').scroll();
        });

        var a = $('.wrapper').height(), b = $('.content')[0].scrollHeight;
        $('.scrollBar').height(Math.floor(a * (a / b)));
    }

    function logE(e) {
                console.log('--------------------------------');
                console.log(e.clientX, e.clientY);
                console.log(e.pageX, e.pageY);
                console.log(e.screenX, e.screenY);
            }
    //    .scrollBarMain {
    //                position: absolute;
    //                left: 290px;
    //                top: 1px;
    //                display: inline-block;
    //                height: 200px;
    //                width: 11px;
    //                background-color: #F0F0F0;
    //                cursor: auto;
    //            }
//
//                .scrollBar {
//                    position: relative;
//                    background-color: gray;
//                    height: 40px;
//                    /*width: 90%;*/
//                    top: 0;
//
//                    border-radius: 4px;
//                    /*box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.5);*/
//                }
})(jQuery);